package com.yoomiito.app.ui.my.remain.transfer;

import android.content.res.Configuration;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.TextView;

import com.jakewharton.rxbinding3.widget.RxTextView;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseLazyFragment;
import com.yoomiito.app.model.my.MyRemainMoneyInfo;
import com.yoomiito.app.model.my.PayeeInfo;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.inputfilter.CashierInputFilter;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.CircleImageView;
import com.yoomiito.app.widget.EditTextAndDel;
import com.yoomiito.app.widget.PasswordView_1;
import com.yoomiito.app.widget.dialog.PswInputDialog;
import com.yoomiito.app.widget.dialog.UserInfoDialog;

import butterknife.BindView;
import butterknife.OnClick;

public class TransferFragment extends BaseLazyFragment<TransferPresenter> {

    @BindView(R.id.user_head_iv)
    CircleImageView mCircleImageView;
    @BindView(R.id.user_name)
    TextView mUserNameTv;
    @BindView(R.id.user_account)
    TextView mUserAccount;
    @BindView(R.id.submit_all)
    TextView mSubmitAllTv;
    @BindView(R.id.tv_tip)
    TextView mTipTv;
    @BindView(R.id.et_money)
    EditTextAndDel mEditTextAndDel;
    @BindView(R.id.transfer)
    Button mButton;
    @BindView(R.id.subtitle)
    TextView mSubtitle;

    private UserInfoDialog userInfoDialog;
    private PswInputDialog pswInputDialog;
    private PayeeInfo payeeInfo;
    private String mRemainMoney;
    private String transferMoney;//转账金额

    @Override
    public int getLayoutId() {
        return R.layout.fm_transfer_money;
    }

    @Override
    public TransferPresenter newP() {
        return new TransferPresenter(App.mAppConfig);
    }

    @Override
    protected void onStartLazy() {
        super.onStartLazy();
        payeeInfo = getArguments().getParcelable(Constant.DATA);
        L.i("结果: " + payeeInfo);
        ImageLoader.getInstance().into(context, payeeInfo.getHeadimgurl(), mCircleImageView);
        mUserNameTv.setText(payeeInfo.getNickname());
        mUserAccount.setText(payeeInfo.getMobile());
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mSubmitAllTv.setVisibility(View.GONE);
        mSubtitle.setText("转账金额");

        mEditTextAndDel.setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
        mEditTextAndDel.setEditFilters(new InputFilter[]{new CashierInputFilter()});
        mEditTextAndDel.setCursorColor(R.drawable.cursor);
        mEditTextAndDel.setTypeface(AllUtils.getTypeface());
        mEditTextAndDel.setIvWidth(DensityUtil.dp2px(16), DensityUtil.dp2px(16));
        mEditTextAndDel.getEditText().setRawInputType(Configuration.KEYBOARD_QWERTY);

        RxTextView.textChanges(mEditTextAndDel.getEditText())
                .subscribe(charSequence -> {
                    if (TextUtils.isEmpty(charSequence.toString())) {
                        setRemainMoney(mRemainMoney);
                        mButton.setEnabled(false);
                        return;
                    }
                    String money = charSequence.toString();
                    if (!AllUtils.moneyCompare(mRemainMoney, money)) {
                        setTip("转账金额超过可提现余额");
                        mButton.setEnabled(false);
                    } else {
                        setRemainMoney(mRemainMoney);
                        mButton.setEnabled(true);
                    }
                });

        getP().getIsAgent();
    }

    @OnClick({R.id.transfer})
    public void onClick(View view) {
        if (view.getId() == R.id.transfer) {
            transferMoney = mEditTextAndDel.getEditText().getText().toString().trim();
            if (SpannableUtils.isEmptyOr0(transferMoney)) {
                YMToast.showText("转账金额不能为0");
                return;
            }
            payeeInfo.setMoney(transferMoney);
            showTransferDialog();
        }
    }

    public void setMoney(MyRemainMoneyInfo data) {
        setRemainMoney(data.getMoney());
    }

    //设置金额
    public void setRemainMoney(String money) {
        dismissLoading();
        mRemainMoney = money;
        mTipTv.setText("可提现余额 ¥" + money);
        mTipTv.setTextColor(ResUtil.getColor(R.color.color_balk_444444));
    }

    public void setTip(String tip) {
        mSubmitAllTv.setVisibility(View.GONE);
        mTipTv.setTextColor(ResUtil.getColor(R.color.color_FA0C07));
        mTipTv.setText(tip);
    }

    public void showTransferDialog() {
        if (userInfoDialog == null) {
            userInfoDialog = new UserInfoDialog(context);
            userInfoDialog.setOnTransferListener(() -> {
                showPsw();
            });
        }
        userInfoDialog.setPayeeInfo(payeeInfo);
        userInfoDialog.show();
    }

    public void showPsw() {
        if (pswInputDialog == null) {
            pswInputDialog = new PswInputDialog(context);
            pswInputDialog.setTextChangedListener(new PasswordView_1.TextChangedListener() {
                @Override
                public void textChanged(CharSequence changeText) {

                }

                @Override
                public void textCompleted(CharSequence text) {
                    getP().transfer(payeeInfo.getId(), transferMoney, text.toString());
                }
            });
        }
        pswInputDialog.show();
    }

    public void error(boolean pswError, String tip) {
        pswInputDialog.showErrorTip(pswError, tip);
        if (!pswError) {
            pswInputDialog.dismiss();
            getP().getIsAgent();
        }
    }

    public void rsaPswError() {
        dismissLoading();
        if (pswInputDialog != null) pswInputDialog.dismiss();
        if (mEditTextAndDel != null) mEditTextAndDel.getEditText().setText("");
        ((TransferActivity) context).switchFm(0);
    }

    public void transferSuccess() {
        dismissLoading();
        pswInputDialog.dismiss();
        ((TransferActivity) context).switchFm(2);
    }


}
